package com.atfcm.stack;

import java.util.Deque;
import java.util.HashSet;
import java.util.LinkedList;

public class T_227_基本计算器 {
    // https://leetcode.cn/problems/basic-calculator-ii/solutions/648647/ji-ben-ji-suan-qi-ii-by-leetcode-solutio-cm28/
    // 双栈
    // 字符串转整数
    // 单个字符转整数
  static    int calculate(String s) {
        Deque<Integer> stack = new LinkedList<>();

        int num = 0;
        char preS = '+';
        for(int i = 0; i < s.length(); i++){
            if(Character.isDigit(s.charAt(i))){
                num = num * 10 + (s.charAt(i) - '0');
            }
            // 数字字符遍历完毕
            if(!Character.isDigit(s.charAt(i)) && s.charAt(i) != ' ' || (i == s.length() -1 ) ){
                switch(preS){
                    case '+':
                        stack.push(num);
                        preS = s.charAt(i);
                        break;
                    case '-':
                        stack.push(-num);
                        preS = s.charAt(i);
                        break;
                    case '*':
                        stack.push(stack.pop() * num);
                        preS = s.charAt(i);
                        break;
                    case '/':
                        stack.push(stack.pop() / num);
                        preS = s.charAt(i);
                        break;

                }

                num = 0;
            }

        }

        int sum2 = 0;
        while(!stack.isEmpty()){
            sum2 += stack.pop();
        }
        return sum2;
    }

    public static void main(String[] args) {
        int calculate = calculate(" 3/2 ");
        System.out.println(calculate);
    }


}
